#!/usr/bin/env python
#  -*- coding: utf-8 -*-
from trade_mdforopenctp import PeopleQuantApi
import time as tm
import zhuchannel
import threading,queue
import os
from concurrent.futures import ThreadPoolExecutor
import traceback
import types
import polars
from datetime import datetime,time,date,timedelta
import copy
from typing import Dict, List, Optional, Tuple, Any


envs = {
    "7x24": {
        "td": "tcp://182.254.243.31:40001",
        "md": "tcp://182.254.243.31:40011",
        "user_id": "",
        "password": "",
        "broker_id": "9999",
        "authcode": "0000000000000000",
        "appid": "simnow_client_test",
        "user_product_info": "",
    },
    "电信1": {
        "td": "tcp://182.254.243.31:30001",
        "md": "tcp://182.254.243.31:30011",
        "user_id": "",
        "password": "",
        "broker_id": "9999",
        "authcode": "0000000000000000",
        "appid": "simnow_client_test",
        "user_product_info": "",
    },
    "电信2": {
        "td": "tcp://182.254.243.31:30002",
        "md": "tcp://182.254.243.31:30012",
        "user_id": "",
        "password": "",
        "broker_id": "9999",
        "authcode": "0000000000000000",
        "appid": "simnow_client_test",
        "user_product_info": "",
    },
    "移动": {
        "td": "tcp://182.254.243.31:30003",
        "md": "tcp://182.254.243.31:30013",
        "user_id": "",
        "password": "",
        "broker_id": "9999",
        "authcode": "0000000000000000",
        "appid": "simnow_client_test",
        "user_product_info": "",
    },
}
TradeFrontAddr="tcp://180.168.146.187:10101"   #交易前置地址
MdFrontAddr="tcp://101.230.209.178:53313"      #行情前置地址
TradeFrontAddr = envs["电信1"]["td"]
MdFrontAddr = envs["电信1"]["md"]
#TradeFrontAddr = envs["7x24"]["td"]
#MdFrontAddr = envs["7x24"]["md"]

#TradeFrontAddr = "tcp://121.37.80.177:20002" #openctp
#MdFrontAddr = "tcp://121.37.80.177:20004" #openctp

BROKERID="9999"   #期货公司ID
USERID=""   #账户
PASSWORD=""   #登录密码
APPID="simnow_client_test"   #客户端ID
AUTHCODE="0000000000000000"  #授权码

#创建api实例
pqapi = PeopleQuantApi(BrokerID=BROKERID, UserID=USERID, PassWord=PASSWORD, AppID=APPID, AuthCode=AUTHCODE, TradeFrontAddr=TradeFrontAddr, MdFrontAddr=MdFrontAddr, s=USERID)
account = pqapi.get_account()            #获取账户资金
position1 = pqapi.get_position("FG601")   #获取合约持仓
quote1 = pqapi.get_quote("FG601")        #获取合约行情
position2 = pqapi.get_position("hc2601")
quote2 = pqapi.get_quote("rb2601")
print(position2)
print(quote1)
#margin1 = pqapi.get_symbol_marginrate("IH2509")
margin2 = pqapi.get_symbol_marginrate("TF2512")  #获取合约保证金
#print(margin1,"IH2509")
print(margin2,"TF2512")
#合约行情时间
UpdateTime = f'{quote2["ActionDay"]} {quote2["UpdateTime"]}.{quote2["UpdateMillisec"]}'
#kline = pqapi.get_kline("FG601", "10m", 10)   #获取K线
#tick = pqapi.get_tick("FG601")                #获取Tick
while True:
    #break
    if UpdateTime != quote1.ctp_datetime: #新行情推送
        print(quote1["InstrumentID"],quote1["LastPrice"],quote1["UpdateTime"],)
        UpdateTime = quote1.ctp_datetime
        print(position1)
        #print(quote2["InstrumentID"],quote2["LastPrice"],quote2["UpdateTime"],)
        #print(position2 )
        #print(account)
        #print(quote2)
        #print(margin2)
        #print(kline.data)
        #print(kline.data.select(["InstrumentID", "period_start",   "open", "high","low", "close", "Volume","OpenInterest","period" ]))
        #print(tick.data.select(["InstrumentID","ActionDay","TradingDay","LastPrice","BidPrice1","AskPrice1","BidVolume1","AskVolume1","ctp_datetime"]).tail(10))
